package com.lhd.web.admin;

import com.lhd.po.Blog;
import com.lhd.po.User;
import com.lhd.service.BlogService;
import com.lhd.service.TagService;
import com.lhd.service.TypeService;
import com.lhd.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuhandong on 2022/4/20.
 */
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;

    @PostMapping("/admin/blog")
    public Integer uploadBlog(@RequestBody Blog blog, HttpSession session){
        User user = (User) session.getAttribute("user");
        if (user != null){
            blog.setUser((User) session.getAttribute("user"));
            blog.setUserId(((User)session.getAttribute("user")).getId());
        }
        if (blog.getTypeId() == null){
            blog.setTypeId(1L);
        }
        blog.setUser((User) session.getAttribute("user"));
        blog.setViews(0);
        List<Long> tagIds = tagService.convertToList(blog.getTagIds());
        Integer b;
        blog.setUpdateTime(new Date());
        if (blog.isPublished()){   //发布动作
            if (blog.getId() == null){   //说明是新博客发布
                blog.setId(blogService.getMaxBlogId()+1);
                blog.setCreateTime(new Date());
                b = blogService.saveBlog(blog);
                tagService.createBlogIdAndTagIds(blog.getId(),tagIds); //保存blogid tagid 关系表
            }{      //说明是已保存博客发布 或者 已发布博客修改后重新发布
                //删除blogid对应的tagid
                tagService.clearBlogIdAndTagId(blog.getId());
                //添加blohgid对应的tagid
                tagService.createBlogIdAndTagIds(blog.getId(),tagIds);
                b = blogService.updateBlog(blog.getId(),blog);
            }
        }else {   //保存动作
            if (blogService.getBlog(blog.getId()) == null){  //说明此时数据库中无此博客，则为新建博客保存
                blog.setId(blogService.getMaxBlogId()+1);
                blog.setCreateTime(new Date());
                b = blogService.saveBlog(blog);
            }else {  //说明数据库中已有此博客信息，为旧博客
                Blog blogInner = blogService.getBlog(blog.getId());
                if (blogInner.isPublished()){   //说明是已发布旧博客修改后保存
                    tagService.clearBlogIdAndTagId(blog.getId());
                    b = blogService.updateBlog(blog.getId(),blog);
                }else {  //说明是未发布旧博客修改后保存
                    b = blogService.updateBlog(blog.getId(),blog);
                }
            }
        }
        return b;
    }

    @GetMapping("/blog/delete/{id}")
    public Integer deleteBlog(@PathVariable Long id){
        blogService.deleteBlog(id);
        tagService.clearBlogIdAndTagId(id);
        return 0;
    }

    @PostMapping("/search/{page}")
    public Map searchBlogBy(@RequestBody BlogQuery blog, @PathVariable Integer page){
        Map<String,Object> map = new HashMap<>();
        Long count = blogService.countBlogByQuery(blog);
        List<Blog> blogs = blogService.listBlog(page, blog);
        for (Blog bl : blogs) {
            if (typeService.getType(bl.getTypeId()) == null){
                bl.setType(typeService.getType(1L));
            }else {
                bl.setType(typeService.getType(bl.getTypeId()));
            }
        }
        // 取满足条件的前五个元素
        int rem = Math.toIntExact(count % 5);
        map.put("page",blogs);
        map.put("curPage",page);
        map.put("preBoolean",page > 0);
        map.put("nexBoolean",page < ((count/5 + (rem == 0 ? 0 : 1))-1));
        return map;
    }
}
